﻿using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Web_Oto.Bases;

namespace Web_Oto.Data
{
    public class IntroductionDal : dalObject
    {
        #region constructors
        public IntroductionDal(string connectionstring, string connectiontype)
            : base(connectionstring, connectiontype)
        {
            
        }

        public IntroductionDal(IDbConnection sqlConnection, string connectiontype, SqlTransaction dbTransaction)
            : base(sqlConnection, connectiontype, dbTransaction)
        {

        }
        #endregion

        #region methods
        public Introduction GetIntroduction()
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                return db.Introductions.FirstOrDefault();
            }
        }

        public int Save(string description)
        {
            try
            {
                using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
                {
                    var introduction = db.Introductions.FirstOrDefault();
                    if (introduction == null)
                    {
                        introduction = new Introduction {Description = description};
                        db.Introductions.InsertOnSubmit(introduction);
                    }
                    else
                        introduction.Description = description;

                    db.SubmitChanges();
                }

                return 1;
            }
            catch
            {
                return -1;
            }
        }
        #endregion
    }
}
